package com.share;

import java.util.ArrayList;

import hipro.user.Users;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.os.Bundle;


public class UserActivity extends BaseActivity {
	private String username;
	private String password;
	private hipro.user.Users webserviceUsers;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		

		// set view events
		webserviceUsers = new Users();
		webserviceUsers.setWebservice(getString(R.string.webservice_user));

		this.setValueAccount();
		//this.updateUser();
		//this.addUser();
		//this.getUserByUsername();
		this.getFriends();
		
	}
	
	public void getFriends(){
		ArrayList<hipro.entities.Users> list = 
			new ArrayList<hipro.entities.Users>();
		list = this.webserviceUsers.getFriends(this.username, this.password);
		
		String label = "";
		
		for(hipro.entities.Users user : list){
			label += user.getUsername();
		}
		AlertDialog al = new AlertDialog.Builder(this).create();
		al.setMessage(label);
		al.show();
	}
	
	public void getUserByUsername(){
		hipro.entities.Users user = new hipro.entities.Users();
		user = this.webserviceUsers.getUserByUsername("admin");
		
		AlertDialog al = new AlertDialog.Builder(this).create();
		al.setMessage(user.getUsername());
		al.show();
	}
	
	public void addUser(){
		hipro.entities.Users users = new hipro.entities.Users();
		users.setUsername("Tien");
		users.setFullname("Ngiye bsdfsdf");
		users.setCountryId(1);
		users.setPassword("123");
		users.setDistance(200);
		users.setImg("dfd.gif");
		
		int id = this.webserviceUsers.addUser(users);
		AlertDialog al = new AlertDialog.Builder(this).create();
		al.setMessage(id+" dfsf");
		al.show();
	}
	
	
	/**
	 * Update user
	 */
	public void updateUser(){
		hipro.entities.Users user = new hipro.entities.Users();
		user = this.webserviceUsers.getUser(this.username, this.password);
		
		user.setFullname("ASSSS");
		
		String rs = this.webserviceUsers.updateUser(this.username, this.password, 
				user);
		AlertDialog al = new AlertDialog.Builder(this).create();
		al.setMessage(rs);
		al.show();
		
	}
	
	/**
	 * Set value account login
	 */
	private void setValueAccount() {
		SharedPreferences settings = getSharedPreferences("pref_login", 0);
		this.username = settings.getString("username", "");
		this.password = settings.getString("password", "");

	}
}
